﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Register 
{
    public partial class FrmBienlai : DevExpress.XtraEditors.XtraForm
    {
        DAL.EntitesDAL.PhieuthuDAL _dal = new DAL.EntitesDAL.PhieuthuDAL();
        BLL.EntitiesBLL.PhieuthuBLL _bll = new BLL.EntitiesBLL.PhieuthuBLL();
        public string _mahv;
        public string _tenhv;
        public long _sotien;
        public FrmBienlai()
        {
            InitializeComponent();
        }

        private void FrmHoadon_Load(object sender, EventArgs e)
        {
            txtMaHVID.Text = _tenhv;
            dteNgaylap.EditValue = DateTime.Now;
            txtSotien.Text = _sotien.ToString();

            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txtSoHD.Text = ma;
                }
            txtGhichu.Focus();
        }

        private void btnInHD_Click(object sender, EventArgs e)
        {
            var o = new DAL.Entities.Phieuthu()
            {
                SoHD = txtSoHD.Text,
                MaHVID = _mahv,
                Ngaylap = Convert.ToDateTime(dteNgaylap.EditValue),
                Sotien = Convert.ToInt64(txtSotien.Text),
                Ghichu = txtGhichu.Text
            };

            var oki = _dal.Insert(o);
            if (oki)
            {
                //MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Report.FrmReport_Bienlai frm = new Report.FrmReport_Bienlai();
                frm._soHD = txtSoHD.Text;
                frm.Show();
                this.Close();
                
            }
            else
            {
                MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSotien.Focus();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuulai_Click(object sender, EventArgs e)
        {
            var o = new DAL.Entities.Phieuthu()
            {
                SoHD = txtSoHD.Text,
                MaHVID = _mahv,
                Ngaylap = Convert.ToDateTime(dteNgaylap.EditValue),
                Sotien = Convert.ToInt64(txtSotien.Text),
                Ghichu = txtGhichu.Text
            };

            var oki = _dal.Insert(o);
            if (oki)
            {
                MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSotien.Focus();
            }
        }

     
    }
}